package com.macro.mall.product.controller;

import com.github.pagehelper.PageInfo;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.PmServerSpec;
import com.macro.mall.product.dto.PmServerSpecDto;
import com.macro.mall.product.service.PmServerSpecService;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.net.URLDecoder;
import java.util.List;

/**
 * 属性管理Controller
 * Created by macro on 2018/4/26.
 */
@Controller
@Api(tags = "PmServerSpecController", description = "服务规格管理")
@RequestMapping("/serverspec")
public class PmServerSpecController {

    @Autowired
    private PmServerSpecService pmServerSpecService;

    @Autowired
    private Mapper mapper;

    @ApiOperation(value = "服务规格列表--不分页")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PmServerSpecDto>> list(@RequestParam(value = "serverSpecName", defaultValue = "") String serverSpecName,
                                                    @RequestParam(value = "isNeedPic", defaultValue = "true") boolean isNeedPic,
                                                    @RequestParam(value = "parentId", defaultValue = "") Long parentId,
                                                    @RequestParam(value = "serverSpecLevel", defaultValue = "") Integer serverSpecLevel
    ) {
        PmServerSpecDto serverSpecDto = new PmServerSpecDto();
        if(!StringUtils.isBlank(serverSpecName)){
            serverSpecName= URLDecoder.decode(serverSpecName);
            serverSpecDto.setServerSpecName(serverSpecName);
        }
        if(parentId != null){
            serverSpecDto.setParentId(parentId);
        }
        if(serverSpecLevel != null){
            serverSpecDto.setServerSpecLevel(serverSpecLevel);
        }
        List<PmServerSpec> pmServerSpecList = pmServerSpecService.list(serverSpecDto,isNeedPic);
        List<PmServerSpecDto> serverSpecDtoList  = pmServerSpecService.converDataList(pmServerSpecList);
        return CommonResult.success(serverSpecDtoList);
    }
    @ApiOperation(value = "服务规格列表--分页")
    @RequestMapping(value = "/listPage", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<PmServerSpecDto>> listPage(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                        @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                        @RequestParam(value = "serverSpecName", defaultValue = "") String serverSpecName,
                                                    @RequestParam(value = "isNeedPic", defaultValue = "true") boolean isNeedPic,
                                                    @RequestParam(value = "parentId") Long parentId,
                                                    @RequestParam(value = "serverSpecLevel") Integer serverSpecLevel
    ) {
        PmServerSpecDto serverSpecDto = new PmServerSpecDto();
        if(!StringUtils.isBlank(serverSpecName)){
            serverSpecName= URLDecoder.decode(serverSpecName);
            serverSpecDto.setServerSpecName(serverSpecName);
        }
        if(parentId != null){
            serverSpecDto.setParentId(parentId);
        }
        if(serverSpecLevel != null){
            serverSpecDto.setServerSpecLevel(serverSpecLevel);
        }
        List<PmServerSpec> pmServerSpecList = pmServerSpecService.listPage(pageNum,pageSize,serverSpecDto,isNeedPic);
        PageInfo<PmServerSpec> pageInfo = new PageInfo<PmServerSpec>(pmServerSpecList);

        List<PmServerSpecDto> dtoList  = pmServerSpecService.converDataList(pmServerSpecList);
        CommonPage<PmServerSpecDto> result = new CommonPage<PmServerSpecDto>();
        result.setPageNum(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        result.setTotal(pageInfo.getTotal());
        result.setTotalPage(pageInfo.getPages());
        result.setList(dtoList);

        return CommonResult.success(result);
    }

    @ApiOperation(value = "保存服务规格")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult save(@Validated @RequestBody PmServerSpecDto pmServerSpecDto, BindingResult result) {
        CommonResult commonResult;
        int count = pmServerSpecService.save(pmServerSpecDto);
        if (count == 1) {
            commonResult = CommonResult.success(count);
        } else {
            commonResult = CommonResult.failed();
        }
        return commonResult;
    }
    @ApiOperation(value = "删除服务规格")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult delete(@RequestParam(value = "serverSpecIds") String serverSpecIds) {
        CommonResult commonResult;
        int count = pmServerSpecService.delete(serverSpecIds);
        if (count > 0) {
            commonResult = CommonResult.success(count);
        } else {
            commonResult = CommonResult.failed();
        }
        return commonResult;
    }
    @ApiOperation(value = "获取单个服务规格包")
    @RequestMapping(value = "/getServerSpecById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmServerSpecDto> getServerSpecById(@RequestParam(value = "serverSpecId", defaultValue = "") Long serverSpecId
    ) {
        PmServerSpec serverSpec = pmServerSpecService.getById(serverSpecId);
        PmServerSpecDto packageDto= pmServerSpecService.converDataInfo(serverSpec);
        return CommonResult.success(packageDto);
    }
}
